package com.zjdiepu.www.biz.yw;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.yw.YwTransferMapper;
import com.zjdiepu.www.ibiz.yw.IYwTransferService;
import com.zjdiepu.www.model.yw.dto.YwTransfer;
import com.zjdiepu.www.model.yw.ext.YwTransferExt;

/**
 * 过户登记Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月09日 09时29分<br/>
 * @since 1.0<br/>
 */
@Service
public class YwTransferServiceImpl extends BaseService implements IYwTransferService {
	public static final Logger logger = LoggerFactory.getLogger(YwTransferServiceImpl.class);

	@Autowired
	private YwTransferMapper ywTransferMapper;
	
	@Override
	public void saveUpdateAndDelete(YwTransfer ywTransfer, OperateType operType) {
		switch (operType) {
		case add:
			ywTransfer.initInsertData(ywTransfer.getModifier(), ywTransfer.getModifierName());
			ywTransferMapper.insert(ywTransfer);
			break;
		case edit:
			ywTransfer.initUpdateData(ywTransfer.getModifier(), ywTransfer.getModifierName());
			// 影响的记录数
			int updateCount = ywTransferMapper.updateByPrimaryKeySelective(ywTransfer);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			ywTransfer.initDeleteData(ywTransfer.getModifier(), ywTransfer.getModifierName());
			// 影响的记录数
			int deleteCount = ywTransferMapper.updateByPrimaryKeySelective(ywTransfer);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<YwTransfer> selectByExt(YwTransferExt ywTransferExt) {
		return ywTransferMapper.selectByExt(ywTransferExt);
	}
	
	@Override
	public int countByExt(YwTransferExt ywTransferExt){
		return ywTransferMapper.countByExt(ywTransferExt);
	}
	
	@Override
	public YwTransfer selectById(String id) {
		return ywTransferMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			ywTransferMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(YwTransfer ywTransfer) {
		return ywTransferMapper.countRefined(ywTransfer);
	}

	@Override
	public YwTransfer selectRefined(YwTransfer ywTransfer) {
		return ywTransferMapper.selectRefined(ywTransfer);
	}
	
	@Override
	public int deletedRefined(YwTransfer ywTransfer) {
		return ywTransferMapper.deletedRefined(ywTransfer);
	}

	@Override
	public boolean repeat(YwTransfer ywTransfer) {
		int count = countRefined(ywTransfer);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(ywTransfer.getId())) {
			return true;
		}
		YwTransfer dbItem = selectRefined(ywTransfer);
		if(null == dbItem) {
			return false;
		}
		return !ywTransfer.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return ywTransferMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<YwTransfer> ywTransfers) {
		return ywTransferMapper.insertBatch(ywTransfers);
	}
}
